package com.flyco.modules.ums.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.common.api.vo.Result;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.ums.entity.UmsMember;
import com.flyco.modules.ums.entity.UmsMemberParam;
import com.flyco.modules.ums.mapper.UmsMemberReceiveAddressMapper;
import com.flyco.modules.ums.model.UmsMemberReceiveAddressModel;
import com.flyco.modules.ums.service.IUmsMemberReceiveAddress;
import com.flyco.modules.ums.service.IUmsMemberService;
import com.flyco.modules.ums.vo.UmsMemberReceiveAddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "会员地址表")
@RestController
@RequestMapping("/f/ums/UmsMemberReceiveAddress")
public class UmsMemberReceiveAddressController extends BaseController<UmsMemberReceiveAddressModel, IUmsMemberReceiveAddress> {

    @Autowired
    private IUmsMemberReceiveAddress umsMemberReceiveAddress;


    @AutoLog(value = "会员地址添加")
    @ApiOperation(value="会员地址表-添加", notes="会员地址表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody UmsMemberReceiveAddressModel umsMemberReceiveAddressModel) {
        umsMemberReceiveAddress.save(umsMemberReceiveAddressModel);
        return Result.ok("添加成功！");
    }

    @AutoLog(value = "会员地址删除")
    @ApiOperation(value="会员地址表-删除", notes="会员地址表-删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        umsMemberReceiveAddress.removeById(id);
        return Result.ok("删除成功！");
    }

    @AutoLog(value = "获取会员地址")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(UmsMemberReceiveAddressModel addressModel,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<UmsMemberReceiveAddressModel> queryWrapper = QueryGenerator.initQueryWrapper(addressModel, req.getParameterMap());
        queryWrapper.orderByAsc("id");
        Page<UmsMemberReceiveAddressModel> page = new Page<UmsMemberReceiveAddressModel>(pageNo, pageSize);
        IPage<UmsMemberReceiveAddressModel> pageList = umsMemberReceiveAddress.page(page, queryWrapper);

        IPage<UmsMemberReceiveAddressVO> pageListVO=new Page<>();
        UmsMemberReceiveAddressVO vo;
        List<UmsMemberReceiveAddressVO>  addressVOList=new ArrayList<>();
        for (UmsMemberReceiveAddressModel model : pageList.getRecords()){

            vo=new UmsMemberReceiveAddressVO();
            BeanUtils.copyProperties(model,vo);
            addressVOList.add(vo);
        }
        pageListVO.setRecords(addressVOList);
        pageListVO.setPages(pageList.getPages());
        pageListVO.setCurrent(pageList.getCurrent());
        pageListVO.setSize(pageList.getSize());
        pageListVO.setTotal(pageList.getTotal());
        return Result.ok(pageListVO);
    }
}
